<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class procurement extends CI_Controller {

    function __construct(){
        parent::__construct();
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("If-Modified-Since: Mon, 22 Jan 2008 00:00:00 GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Cache-Control: private");
		header("Pragma: no-cache");
		$this->auth	= unserialize(base64_decode($this->session->userdata('sisfodealer')));
		$this->modul='procurement';
		$this->load->model('mprocurement');
		if(!$this->auth){
			$data['pesan'] = "<font color='red'>You Must Login!..</font>";
			$this->load->view('login',$data);
			return;
		}
	}

	public function index(){
		if($this->auth){
			$data=$this->auth;
			$data['modul']=$this->modul;
			$this->load->view('home');
		}else{
			$data['pesan'] = "<font color='red'>You Must Login!..</font>";
			$this->load->view('login',$data);
			return;
		}
	}
	
	function get_grid($p1){
		$data=$this->auth;
		$data['modul_main']=$this->input->post('main_modul');
		$data['judul']=$this->input->post('judul');
		$data['flag_div']=$this->input->post('flag_div');
		$data['modul_anak']=$p1;
		$this->load->view($this->modul.'/grid',$data);
	}
	
	function get_form($p1){
		$data=$this->auth;
		//$data['modul_main']=$this->input->post('main_modul');
		//$data['judul']=$this->input->post('judul');
		//print_r($data);exit;
		$data['modul']=$p1;
		$data['sts']=$this->input->post('sts');
		$data['flag_div']=$this->input->post('flag_div');
		switch($p1){
			case "pemesanan":
				$kode='PSS-';
				$no=$this->mprocurement->get_data('get_nopemesanan');
				if($no < 10){$digit='00000'.$no;}
				if($no >= 10 && $no < 100){$digit='0000'.$no;}
				if($no >= 100 && $no < 1000){$digit='000'.$no;}
				if($no >= 1000 && $no < 10000){$digit='00'.$no;}
				if($no >= 10000 && $no < 100000){$digit='0'.$no;}
				if($no >= 100000 && $no < 1000000){$digit=$no;}
				$no_pem_cab=$this->input->post('no_pemesanan_cabang');
				$id_pem_cab=$this->input->post('id_pemesanan_cabang');
				if($data['sts']=='edit'){
					$data['data_header']=$this->mprocurement->get_data('pemesanan');
					$data['data_detil']=$this->mprocurement->get_data('detil_pemesanan');
					$data['jml_detil']=count($data['data_detil']);
					$data['no_pesanan']=$data['data_header']->no_pemesanan;
				}
				else{
					$data['no_pesanan']=$kode.$digit;
					$data['detil_pemesanan_cabang']=$this->mprocurement->get_data('detil_pemesanan_cabang',$id_pem_cab);
					//$data['tgl_pemesanan_cabang']=$this->input->post('tgl_pemesanan_cabang');
				}
				$sql="SELECT A.*,B.nama_dealer FROM tbl_pemesanan_cabang A
						LEFT JOIN tbl_master_dealer B ON A.id_dealer=B.id WHERE A.no_pemesanan='".$no_pem_cab."'";
				$data['data_cab']=$this->db->query($sql)->row();
				
				
				
				
					
			break;
			case "pemesanan_cabang":
				$kode='PSC-';
				$no=$this->mprocurement->get_data('get_nopemesanan');
				if($no < 10){$digit='00000'.$no;}
				if($no >= 10 && $no < 100){$digit='0000'.$no;}
				if($no >= 100 && $no < 1000){$digit='000'.$no;}
				if($no >= 1000 && $no < 10000){$digit='00'.$no;}
				if($no >= 10000 && $no < 100000){$digit='0'.$no;}
				if($no >= 100000 && $no < 1000000){$digit=$no;}
				
				if($data['sts']=='edit'){
					$data['data_header']=$this->mprocurement->get_data('pemesanan_cabang');
					$data['data_detil']=$this->mprocurement->get_data('detil_pemesanan_cabang');
					$data['jml_detil']=count($data['data_detil']);
					$data['no_pesanan']=$data['data_header']->no_pemesanan;
				}
				else{$data['no_pesanan']=$kode.$digit;}
				$data['dealer']=$this->db->get('tbl_master_dealer')->result_array();
				
					
			break;
			
			case "pembelian":
					if($data['sts']=='edit'){
						$data['data_header']=$this->mprocurement->get_data('faktur');
						//$data['data_pembayaran']=$this->mprocurement->get_data('detil_pembayaran');
						$data['data_detil']=$this->mprocurement->get_data('detil_faktur');
						$data['jml_detil']=count($data['data_detil']);
						$data['data_barang']=$this->get_table('detil_pemesanan_supplier','html',$data['data_header']->tbl_pemesanan_supplier_id);
					//	$data['no_pesanan']=$data['data_header']->no_pemesanan;
					}
			break;
			
			case "penerimaan":
				$data['gudang']=$this->mprocurement->get_data('gudang');
				$kode='GUD-';
				$no=$this->mprocurement->get_data('get_nopenerimaan');
				if($no < 10){$digit='00000'.$no;}
				if($no >= 10 && $no < 100){$digit='0000'.$no;}
				if($no >= 100 && $no < 1000){$digit='000'.$no;}
				if($no >= 1000 && $no < 10000){$digit='00'.$no;}
				if($no >= 10000 && $no < 100000){$digit='0'.$no;}
				if($no >= 100000 && $no < 1000000){$digit=$no;}
				
				if($data['sts']=='edit'){
					$data['data_header']=$this->mprocurement->get_data('penerimaan');
					//$data['data_detil']=$this->mprocurement->get_data('detil_penerimaan');
					$data['jml_detil']=0;
					$data['no_penerimaan']=$data['data_header']->no_penerimaan;
				}
				else{$data['no_penerimaan']=$kode.$digit;}
			break;
			
			case "retur":
				$data['group_dealer']=$this->mprocurement->get_data('group_dealer');
				$data['dealer']=$this->mprocurement->get_data('dealer');
				$data['gudang']=$this->mprocurement->get_data('gudang');
				$kode='RET-';
				$no=$this->mprocurement->get_data('get_noretur');
				if($no < 10){$digit='00000'.$no;}
				if($no >= 10 && $no < 100){$digit='0000'.$no;}
				if($no >= 100 && $no < 1000){$digit='000'.$no;}
				if($no >= 1000 && $no < 10000){$digit='00'.$no;}
				if($no >= 10000 && $no < 100000){$digit='0'.$no;}
				if($no >= 100000 && $no < 1000000){$digit=$no;}
				
				if($data['sts']=='edit'){
					$data['data_header']=$this->mprocurement->get_data('retur_pembelian');
					//$data['data_detil']=$this->mprocurement->get_data('detil_penerimaan');
					$data['jml_detil']=0;
					$data['no_retur']=$data['data_header']->no_retur;
				}
				else{$data['no_retur']=$kode.$digit;}
			break;
			
			case "subdetil_penerimaan":
				$data['id_penerimaan']=$this->input->post('id_penerimaan');
				return $this->load->view($this->modul.'/subdetil_penerimaan',$data);
			break;
		}
		$this->load->view($this->modul.'/form',$data);
	}
	
	function get_main($p1="",$p2=""){
		$data=$this->auth;
		switch ($p1){
			case "div_unit":
				$data['judul']='Unit';
				$data['flag_div']='U';
			break;
			case "div_service":
				$data['judul']='Service';
				$data['flag_div']='V';
			break;
			case "div_generator":
				$data['judul']='Generator';
				$data['flag_div']='G';
			break;
			case "div_sparepart":
				$data['judul']='Spare Part';
				$data['flag_div']='S';
			break;
			case "div_promosi":
				$data['judul']='Promosi';
				$data['flag_div']='P';
			break;
		}
		$data['modul']=$p1;
		$this->load->view($this->modul.'/main',$data);
	}
	
	function get_data($p1=""){
		echo $this->mprocurement->get_data($p1);
	}
	
	function simpan($p1=""){
		$post = array();
		foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		echo $this->mprocurement->simpan($p1,$post);
	}
	
	function hapus($p1=""){
		echo $this->mprocurement->hapus($p1);
	}
	
	function get_databarang(){
		$sts=$this->input->post('sts');
		if($sts=='edit'){
			$data_pesanan=$this->mprocurement->get_data('detil_penerimaan');
		}
		else{$data_pesanan=$this->mprocurement->get_data('detil_pemesanan');}
		$html=' <table width="100%" border="0" id="table-2">
						 	<thead>
							  <tr>
								<td width="150px;">Nama Barang&nbsp;</th>
								<th>Ketegori Barang&nbsp;</th>
								<td align="center">Qty&nbsp;</th>
								<th>Satuan&nbsp;</th>
								<th>Qty Diterima</th>
								<th>Qty Sisa</th>
							  </tr>
							  </thead>';
		
		foreach($data_pesanan as $v){
			$html .='<tr>
					<td width="350px">'.$v['nama_barang'].'<input type="hidden" name="id_barang[]" value="'.$v['id_barang'].'" >
						<input type="hidden" name="sts_detil[]" value="'.$sts.'" >
						<input type="hidden" name="id_detil[]" value="'.($sts=='edit' ? $v['id'] : '').'" >
					</td>
					<td width="150px">'.$v['kategori_barang'].'</td>
					<td width="50px">'.$v['qty'].'<input type="hidden" name="qty[]" value="'.$v['qty'].'" ></td>
					<td width="50px">'.$v['satuan_barang'].'</td>
					<td width="50px" align="center">
						<input type="text" name="qty_diterima[]"  style="width:30px;"  class="angka" value="'.($sts=='edit' ? $v['qty_diterima'] : '').'"></td>
					<td width="50px" align="center">
					<input type="text" name="qty_sisa[]" style="width:30px;"  class="angka" value="'.($sts=='edit' ? $v['qty_sisa'] : '').'" ></td>
			</tr>';
		}
							  
		$html .='</table>';
		
		echo $html;
	}
	
	function get_table($p1="",$p2="",$p3=""){
		$html ='';
		switch ($p1){
			case "subdetil_penerimaan":
				$qty=$this->input->post('qty');
				$id_sub=$this->input->post('id');
				$combo=$this->db->get('idx_warna')->result_array();
				$opt="";
				foreach($combo as $v){
					$opt .='<option value="'.$v['id'].'">'.$v['warna'].'</option>';
				}
				
				$status_na=$this->db->get_where('tbl_subdetil_item_penerimaan',array('tbl_subdetil_penerimaan_id'=>$id_sub))->result_array();
			//	print_r($status_na);
				
				if(count($status_na)>0){
					$sts_na='edit';
				}
				else{$sts_na='add';}
				
				$html=' <form id="form_subdetil" method="post" url="'.base_url().'procurement/simpan/tbl_subdetil_item_penerimaan">
					<table width="100%" border="0" id="table-2">
						 	<thead>
							  <tr>
							  	<td width="30px;">No <input type="hidden" name="sts" value=""> </th>
								<td width="150px;">Nama Barang <input type="hidden" name="tbl_subdetil_penerimaan_id" value="'.$id_sub.'"></th>
								<th>Ketegori Barang&nbsp;</th>
								<td align="center">Qty&nbsp;</th>
								<th>Satuan&nbsp;</th>
								<th>Warna</th>
								<th>No Mesin</th>
								<th>No Rangka</th>
								<th>Tahun</th>
							  </tr>
							  </thead>';
				$no=1;
				for($i=0;$i<$qty;$i++){
					if($no > count($status_na)){
						$html .=' <tr>
						<td>'.$no.' <input type="hidden" name="sts_detil[]" value="add"></td>
						<td>'.$this->input->post('nama').'</td>
						<td>'.$this->input->post('kategori').'</td>
						<td>1</td>
						<td>'.$this->input->post('satuan').' <input type="hidden" name="satuan[]" value="'.$this->input->post('satuan').'"></td>
						<td align="center">
						<select name="cl_warna_id[]" style="width:100px;border: 1px solid silver; padding: 1px;">
							<option value="" selected>-- Pilih --</option>';
							foreach($combo as $v){
								$html .='<option value="'.$v['id'].'">'.$v['warna'].'</option>';
							}
						$html .='	
						</select>
						</td>
						<td align="center"><input type="text" name="no_mesin[]" style="width:100px; border: 1px solid silver; padding: 1px;" value=""></td>
						<td align="center"><input type="text" name="no_rangka[]" style="width:100px; border: 1px solid silver; padding: 1px;" value=""><input type="hidden" name="id_detilsub[]" value=""></td>
						<td align="center"><input type="text" name="tahun[]" style="width:100px; border: 1px solid silver; padding: 1px;" value=""></td>
					</tr>';
					}
					else{
					$html .=' <tr>
						<td>'.$no.' <input type="hidden" name="sts_detil[]" value="'.$sts_na.'"></td>
						<td>'.$this->input->post('nama').'</td>
						<td>'.$this->input->post('kategori').'</td>
						<td>1</td>
						<td>'.$this->input->post('satuan').' <input type="hidden" name="satuan[]" value="'.$this->input->post('satuan').'"></td>
						<td align="center">
						<select name="cl_warna_id[]" style="width:100px;border: 1px solid silver; padding: 1px;">
							<option value="" selected>-- Pilih --</option>';
							foreach($combo as $v){
								$html .='<option value="'.$v['id'].'" '; if($sts_na=='edit'){if($v['id']==$status_na[$i]['cl_warna_id']){$html .='selected';}} $html .='>'.$v['warna'].'</option>';
							}
						$html .='	
						</select>
						</td>
						<td align="center"><input type="text" name="no_mesin[]" style="width:100px; border: 1px solid silver; padding: 1px;" value="'.($sts_na=='edit' ? $status_na[$i]['no_mesin'] : '').'"></td>
						<td align="center"><input type="text" name="no_rangka[]" style="width:100px; border: 1px solid silver; padding: 1px;" value="'.($sts_na=='edit' ? $status_na[$i]['no_rangka'] : '').'"><input type="hidden" name="id_detilsub[]" value="'.($sts_na=='edit' ? $status_na[$i]['id'] : '').'"></td>
						<td align="center"><input type="text" name="tahun[]" style="width:100px; border: 1px solid silver; padding: 1px;" value="'.$status_na[$i]['tahun'].'"></td>
					</tr>';
					}
					$no++;
				}
				$html .='</table></form>';			  
			break;
			case "detil_pemesanan_supplier":
				$modul=$this->input->post('modul');
				$html='<table width="100%" border="0" id="table-2">
						 	<thead>
							  <tr>
								<th width="450px;">Nama Barang&nbsp;</th>
								<th align="center">Qty&nbsp;</th>
								<th>Satuan&nbsp;</th>
								<th>Harga Satuan&nbsp;</th>
								<th>Tot. Harga&nbsp;</th>
								
							  </tr>
							  </thead>';
				$data_detil=$this->mprocurement->get_data('detil_pemesanan',$p3);
				$idx=0;
				if($p2!=''){
					foreach($data_detil as $v){
					$html .='
							<tr>
								<td width="150px;" align="center">
									
									'.$v['nama_barang'].'
								</td>
								<td align="right">'.number_format($v['qty'],2).'</td>
								<td>'.$v['satuan_barang'].'</span></td>
								<td align="right"><span id="harga_na_'.$modul.'_'.$idx.'">'.number_format($v['nominal_harga'],2).'</span></td>
								<td align="right"><span id="tot_harga_na_'.$modul.'_'.$idx.'"  class="tot_na_'.$modul.'" val="'.$v['tot_harga'].'">'.number_format($v['tot_harga'],2).'</span></td>
								
							  </tr>
					';
					$idx++;
					}
				}
				else{
					foreach($data_detil as $v){
					$html .='
							<tr>
								<td width="150px;" align="center">
									<input type="hidden" name="id_barang[]" value="'.$v['tbl_master_barang_id'].'">
									<input type="hidden" name="id_detil[]" value="" >
									<input type="hidden" name="sts_detil[]" value="add" >
									'.$v['nama_barang'].'
								</td>
								<td align="right"><input type="hidden" name="qty[]"  value="'.$v['qty'].'">
									'.number_format($v['qty'],2).'</td>
								<td>'.$v['satuan_barang'].'</span></td>
								<td align="right"><span id="harga_na_'.$modul.'_'.$idx.'">'.number_format($v['nominal_harga'],2).'</span></td>
								<td align="right"><span id="tot_harga_na_'.$modul.'_'.$idx.'"  class="tot_na_'.$modul.'" val="'.$v['tot_harga'].'">'.number_format($v['tot_harga'],2).'</span></td>
								
							  </tr>
					';
					$idx++;
					}
				}
				
				$html .='</table>';
				if($p2!=''){return $html;}			  
			break;
			
			case "detil_penerimaan":
				$modul=$this->input->post('modul');
				$data_detil=$this->mprocurement->get_data('data_detil_penerimaan',$p3);
				$html='<table width="100%" border="0" id="table-2">
						 	<thead>
							  <tr>
								<th width="450px;">Nama Barang&nbsp;</th>
								<th align="center">Qty Faktur&nbsp;</th>
								<th align="center">Qty Diterima&nbsp;</th>
								<th align="center">Qty Masuk&nbsp;</th>
								<th align="center">Qty Sisa&nbsp;</th>
								<th>Satuan&nbsp;</th>
							  </tr>
							  </thead>';
				$idx=0;
				
				foreach($data_detil as $v){
					if($v['id_penerimaan']==""){$sts="add";}else{$sts="edit";}
					$html .='<tr>
								<td width="450px;"><input type="hidden" id="kode_barang_'.$modul.'_'.$idx.'" name="id_barang[]" value="'.$v['tbl_master_barang_id'].'">'.$v['nama_barang'].'&nbsp;</td>
								<td align="center"><input type="hidden" name="qty_faktur_na[]" value="'.$v['qty'].'">'.$v['qty'].'&nbsp;</td>
								<td align="center"><input type="hidden" name="qty_diterima[]"  value="'.$v['qty_diterima'].'">'.$v['qty_diterima'].'&nbsp;</td>
								<td align="center">
									<input type="text" style="width:30px;" onchange="cek_sisa('.$idx.');"  class="angka cek"  name="qty[]" id="qty_'.$modul.'_'.$idx.'">&nbsp;</td>
								<td align="center"><input type="hidden" id="qty_sisa_'.$idx.'" name="qty_sisa[]" value="'.$v['qty_sisa'].'">'.$v['qty_sisa'].'&nbsp;</td>
								<td>
								<input type="hidden" name="id_detil[]" value="'.$v['id_subdetil'].'" >
								<input type="hidden" name="sts_detil[]" value="'.$sts.'" >
								'.$v['satuan_barang'].'&nbsp;</td>
							  </tr>';
				
					$idx++;
				}
				$html .='</table>';
				if($p2!=''){return $html;}	
			break;
			case "detil_retur":
				$modul=$this->input->post('modul');
				$data_detil=$this->mprocurement->get_data('data_detil_penerimaan',$p3);
				$html='<table width="100%" border="0" id="table-2">
						 	<thead>
							  <tr>
								<th width="450px;">Nama Barang&nbsp;</th>
								<th align="center">Qty Faktur&nbsp;</th>
								<th align="center">Qty Diterima&nbsp;</th>
								<th align="center">Qty Sisa&nbsp;</th>
								<th align="center">Qty Reject&nbsp;</th>
								<th align="center">Satuan&nbsp;</th>
								<th align="center">Harga&nbsp;</th>
								<th align="center">Total&nbsp;</th>
								
							  </tr>
							  </thead>';
				$idx=0;
				
				foreach($data_detil as $v){
					//if($v['id_penerimaan']==""){$sts="add";}else{$sts="edit";}
					$sts="add";
					$html .='<tr>
								<td width="450px;"><input type="hidden" id="kode_barang_'.$modul.'_'.$idx.'" name="id_barang[]" value="'.$v['tbl_master_barang_id'].'">'.$v['nama_barang'].'&nbsp;</td>
								<td align="center">'.$v['qty'].'&nbsp;</td>
								<td align="center">'.$v['qty_diterima'].'&nbsp;</td>
								
								<td align="center">'.$v['qty_sisa'].'&nbsp;</td>
								<td align="center">
									<input type="text" style="width:30px;"  class="angka cek"  name="qty[]" id="qty_'.$modul.'_'.$idx.'">
								</td>
								<td>
								<input type="hidden" name="id_detil[]" value="'.$v['id_subdetil'].'" >
								<input type="hidden" name="sts_detil[]" value="'.$sts.'" >
								'.$v['satuan_barang'].'&nbsp;</td>
								
								<td align="right">Rp. '.number_format($v['nominal_harga']).'</td>
								<td align="right">Rp. '.number_format(($v['nominal_harga']*$v['qty_diterima'])).'&nbsp;</td>
							  </tr>';
				
					$idx++;
				}
				$html .='</table>';
				if($p2!=''){return $html;}	
			break;
		}
		
		echo $html;
	}
	
	function datapendukung($p1=""){
		$data=array();
		$data['type']=$p1;
		$data['modul']=$this->input->post('modul');
		$this->load->view('procurement/windowna',$data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */